package com.itheima.core.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.common.utils.Page;
import com.itheima.core.po.BaseDict;
import com.itheima.core.po.Customer;
import com.itheima.core.service.iservice.BaseDictService;
import com.itheima.core.service.iservice.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class CustomerController {

    @Autowired
    CustomerService customerService;
    @Autowired
    BaseDictService baseDictService;
    @Value("${customer.from.type}")
    private String FROM_TYPE;
    @Value("${customer.industry.type}")
    private String INDUSTRY_TYPE;
    @Value("${customer.level.type}")
    private String LEVEL_TYPE;

    private Integer now_edit_cust_id = 0;

    @RequestMapping("/customer/list.action")
    public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows, String custName, String custSource,String custIndustry, String custLevel, Model model){
        Page<Customer> customers = customerService.findCustomerList(page,rows,custName,custSource,custIndustry,custLevel);

        model.addAttribute("page",customers);
        List<BaseDict> fromType = baseDictService.findBaseDictByTypeCode(FROM_TYPE);
        System.out.println(fromType);
        List<BaseDict> industryType = baseDictService.findBaseDictByTypeCode(INDUSTRY_TYPE);
        List<BaseDict> levelType = baseDictService.findBaseDictByTypeCode(LEVEL_TYPE);
        model.addAttribute("fromType",fromType);
        model.addAttribute("industryType",industryType);
        model.addAttribute("levelType",levelType);
        model.addAttribute("custName",custName);
        model.addAttribute("custSource",custSource);
        model.addAttribute("custIndustry",custIndustry);
        model.addAttribute("custLevel",custLevel);
        return "customer";
    }

    @ResponseBody
    @RequestMapping("/customer/update.action")
    public String update(Customer customer){
        customer.setCust_id(now_edit_cust_id);
        System.out.println(customer);
        return "update success";
    }

    @ResponseBody
    @RequestMapping(value = "/customer/edit.action",produces = "text/html; charset=UTF-8" )
    public String edit(Integer id, HttpServletResponse response) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println("我进来了");
        System.out.println(id);
        now_edit_cust_id = id;
        List<Customer> customers = customerService.findByCustId(id);
        String data = mapper.writeValueAsString(customers.get(0));
        System.out.println(data);


        return data;
    }

    @ResponseBody
    @RequestMapping("/customer/delete.action")
    public String delete(Integer id){
        int state = customerService.deleteById(id);
        System.out.println(state);
        if(state == 1){
            return "delete success";
        }
        return "delete fail";
    }

    @ResponseBody
    @RequestMapping("/customer/create.action")
    public String create(Customer customer){
        System.out.println("新建客户：" + customer);
        int state = customerService.createCustomer(customer);
        System.out.println(state);
        if(state == 1){
            return "create success";
        }
        return "create fail";
    }

}
